package org.meluk.billboard.jpa.seabattle;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.MappedSuperclass;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Transient;
import org.meluk.billboard.jpa.BaseGame;
import org.meluk.billboard.game.seaBattle.GameState;
import org.meluk.billboard.jpa.Player;

@Entity
public abstract class SBGame extends BaseGame {

    public SBGame() {
    }

    public SBGame(Player player1, Player player2) {
    }

    public SBGame(SBBoard board1, SBBoard board2, Player player1, Player player2) {
        this.board1 = board1;
        this.board2 = board2;
        boards.add(board1);
        boards.add(board1);
    }
    //board of player1
    @Transient
    private SBBoard board1;
    //board of player2
    @Transient
    private SBBoard board2;
    @Enumerated(EnumType.STRING)
    private GameState gameState;
    @OneToMany(mappedBy = "game")
    private List<SBBoard> boards = new ArrayList<SBBoard>();
    @ManyToMany
    @JoinTable(name = "sbgame_player", joinColumns = {
        @JoinColumn(name = "game_id")},
    inverseJoinColumns = {
        @JoinColumn(name = "player_id")
    })
    private List<Player> players = new ArrayList<Player>();

    public void addBoard(SBBoard board) {
        if (!getBoards().contains(board)) {
            if (board.getGame() != null) {
                board.getGame().getBoards().remove(board);
            }
            boards.add(board);
            board.setGame(this);
        }
    }

    public SBBoard getBoard1() {
        return board1;
    }

    public void setBoard1(SBBoard board1) {
        this.board1 = board1;
    }

    public SBBoard getBoard2() {
        return board2;
    }

    public void setBoard2(SBBoard board2) {
        this.board2 = board2;
    }

    public GameState getGameState() {
        return gameState;
    }

    public void setGameState(GameState gameState) {
        this.gameState = gameState;
    }

    public List<SBBoard> getBoards() {
        return boards;
    }

    public List<Player> getPlayers() {
        return players;
    }
}
